import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

/* 整体页面布局Layout */
import Layout from '@/components/layout'

/**
 * hidden: true                   if `hidden:true` will not show in the sidebar(default is false)
 * alwaysShow: true               if set true, will always show the root menu, whatever its child routes length
 *                                if not set alwaysShow, only more than one route under the children
 *                                it will becomes nested mode, otherwise not show the root menu
 * redirect: noredirect           if `redirect:noredirect` will no redirct in the breadcrumb
 * name:'router-name'             the name is used by <keep-alive> (must set!!!)
 * meta : {
    roles: ['super','editor']    control the page roles (you can set multiple roles)
    title: 'title'               the name show in sidebar and breadcrumb (recommend set)
    icon: 'svg-name'/'el-icon-x' the icon show in the sidebar
    breadcrumb: false            if set false, the item will hidden in breadcrumb(default is true)
    activeMenu: '/example/list'  if set path, the sidebar will highlight the path you set
  }
 **/

/**
 * constantRoutes
 * a base page that does not have permission requirements
 * all roles can be accessed
 */
export const constantRoutes = [
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true,
    meta: {
      title: '登录页'
    }
  },
  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true,
    meta: {
      title: '错误页'
    }
  },
  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    name: 'Dashboard',
    children: [{
      path: '/dashboard',
      component: () => import('@/views/home/index'),
      meta: { title: '首页', icon: 'home', breadcrumb: false }
    }]
  },

]

/**
 * 动态路由:特定权限访问页面
 */
export const asyncRouter = [
  {
    path: '/resource',
    component: Layout,
    redirect: '/course', //从定向路径
    name: 'Resource',
    meta: {
      title: '资源管理',
      icon: 'resource',
      roles: ['super', 'institute', 'test'],
    },
    children: [
      {
        path: '/course',
        component: () => import('@/views/resource/course/index'), // Parent router-view
        redirect: '/courseList', //从定向路径
        name: 'Course',
        meta: { title: '课程管理', roles: ['super', 'institute', 'test'], icon: 'courseList' },
        children: [
          {
            path: '/courseList',
            component: () => import('@/views/resource/course/courseList'),
            name: 'CourseList',
            meta: { title: '课程列表', roles: ['super', 'institute', 'test'], icon: 'courseList' },
          },
          {
            path: '/info',
            component: () => import('@/views/resource/course/info'),
            name: 'AddCourse',
            meta: { title: '添加课程', roles: ['super', 'institute'], icon: 'tree' },
            hidden: true,
          },
          {
            path: '/info/:id',
            component: () => import('@/views/resource/course/info'),
            name: 'EduCourseInfoEdit',
            meta: { title: '编辑课程基本信息', roles: ['super', 'institute'], noCache: true },
            hidden: true
          },
          {
            path: '/chapter/:id/:courseId/:gradeId/:kind',
            component: () => import('@/views/resource/course/chapter'),
            name: 'EduCourseChapterEdit',
            meta: { title: '编辑课程目录', roles: ['super', 'institute'], noCache: true },
            hidden: true
          },
          {
            path: '/chapterQuestion',
            component: () => import('@/views/resource/course/chapterQuestion'),
            name: 'ChapterQuestion',
            meta: { title: '小节题目', roles: ['super', 'institute'], noCache: true },
            hidden: true
          },
          {
            path: '/publish/:id',
            component: () => import('@/views/resource/course/publish'),
            name: 'EduCoursePublishEdit',
            meta: { title: '发布课程', roles: ['super', 'institute'], noCache: true },
            hidden: true
          },
        ]
      },

      // {
      //   path: '/video',
      //   component: () => import('@/views/resource/video/index'), // Parent router-view
      //   name: 'Video',
      //   meta: { title: '独立视频', roles: ['super', 'institute'], icon: 'video' },
      // },
      {
        path: '/courseware',
        component: () => import('@/views/resource/document/index'),
        name: 'Courseware',
        meta: { title: '国家标准', roles: ['super', 'institute'], icon: 'document' },
      },
      {
        path: '/video/studyRecord',//视频学习记录
        component: () => import('@/views/resource/video/studyRecord'),
        name: 'VideoStudyRecord',
        hidden: true,
        meta: {
          title: '查看视频学习记录', roles: ['super', 'institute'],
        }
      },
      {
        path: '/questionBank',
        name: 'QuestionBank',
        component: () => import('@/views/resource/questionBank/index'),
        redirect: '/questionBankList', //从定向路径
        meta: { title: '题库管理', roles: ['super', 'institute'], icon: 'questionBank', breadcrumb: true },
        children: [
          {
            path: '/questionBankList',//试卷信息列表
            component: () => import('@/views/resource/questionBank/questionBankList'),
            name: 'QuestionBankList',
            meta: { title: '题库列表', roles: ['super', 'institute'], icon: 'questionBank' },
          },
          {
            path: '/question',//试卷内部试题页面
            component: () => import('@/views/resource/questionBank/question'),
            name: 'Question1',
            hidden: true,
            meta: {
              title: '查看题目', roles: ['super', 'institute'],
            }
          },
        ]
      },
      {
        path: '/testPaper',
        name: 'TestPaper',
        component: () => import('@/views/resource/testPaper/index'),
        redirect: '/testPaperList', //从定向路径
        meta: { title: '试卷管理', roles: ['super', 'institute'], icon: 'testPaper', breadcrumb: true },
        children: [
          {
            path: '/testPaperList',//试卷信息列表
            component: () => import('@/views/resource/testPaper/testPaperList'),
            name: 'TestPaperList',
            meta: { title: '试卷列表', roles: ['super', 'institute'], icon: 'testPaper' },
          },
          {
            path: '/question',//试卷内部试题页面
            component: () => import('@/views/resource/testPaper/question'),
            name: 'Question2',
            hidden: true,
            meta: {
              title: '查看试题', roles: ['super', 'institute'],
            }
          },
          {
            path: '/testRecord',//试卷内学员做题记录
            component: () => import('@/views/resource/testPaper/testRecord'),
            name: 'TestRecord',
            hidden: true,
            meta: {
              title: '查看做题记录', roles: ['super', 'institute'],
            }
          },
        ]
      },
      {
        path: '/classification',
        component: () => import('@/views/resource/classification/index'),
        name: 'Classification',
        meta: { title: '工种分类', roles: ['super', 'institute'], icon: 'category' }
      },
    ]
  },
  {
    path: '/student',
    component: Layout,
    redirect: '/list',
    name: 'Student',
    alwaysShow: false,
    meta: {
      title: '学员管理', roles: ['super', 'institute'], icon: 'student'
    },
    children: [
      {
        path: '/list',
        component: () => import('@/views/student/list/index'),
        name: 'List',
        meta: { title: '学员列表', roles: ['super', 'institute'], icon: 'studentList', },
      },
      {
        path: '/studentCertificate',
        component: () => import('@/views/student/studentCertificate/index'),
        name: 'StudentCertificate',
        meta: { title: '学员证书', roles: ['super', 'institute'], icon: 'recordList' },
        hidden: true,
      },
      {
        path: '/videoRecord',//查看学员视频学习记录
        component: () => import('@/views/student/videoRecord/index'),
        name: 'VideoRecord',
        hidden: true,
        meta: {
          title: '学员视频学习记录', roles: ['super', 'institute'],
        }
      },
      {
        path: '/questionRecord',//学员做题学习记录
        component: () => import('@/views/student/questionRecord/index'),
        name: 'QuestionRecord',
        hidden: true,
        meta: {
          title: '学员做题学习记录', roles: ['super', 'institute'],
        }
      },
    ]
  },
  {
    path: '/class',
    component: Layout,
    redirect: '/classList',
    name: 'Class',
    meta: { title: '班级管理', roles: ['super', 'institute'], icon: 'class' },
    children: [
      {
        path: '/classList',
        component: () => import('@/views/class/classList'),
        name: 'ClassList',
        meta: { title: '班级列表', roles: ['super', 'institute'], icon: 'classList' }
      },
      {
        path: '/classStudent',
        component: () => import('@/views/class/classStudent'),
        name: 'ClassStudent',
        meta: { title: '班级学生列表', roles: ['super', 'institute'], icon: 'recordList' },
        hidden: true,
      },
      {
        path: '/classResource',
        component: () => import('@/views/class/classResource'),
        name: 'ClassResource',
        meta: { title: '班级课件列表', roles: ['super', 'institute'], icon: 'recordList' },
        hidden: true,
      },
      {
        path: '/classCourse',
        component: () => import('@/views/class/classCourse'),
        name: 'ClassCourse',
        meta: { title: '班级课程列表', roles: ['super', 'institute'], icon: 'recordList' },
        hidden: true,
      },
    ]
  },
  {
    path: '/certificate',
    component: Layout,
    redirect: '/certificateList',
    name: 'Certificate',
    alwaysShow: false,
    meta: { title: '证书管理', roles: ['super', 'institute'], icon: 'certificate' },
    children: [
      {
        path: '/certificateList',
        component: () => import('@/views/certificate/index'),
        name: 'CertificateList',
        meta: { title: '证书列表', roles: ['super', 'institute'], icon: 'certificateList', }
      }
    ]
  },

  {
    path: '/website',
    component: Layout,
    redirect: '/banner',
    name: 'Website',
    meta: {
      title: '平台首页',
      icon: 'website',
      roles: ['super', 'institute'],
    },
    children: [
      {
        path: '/banner',
        component: () => import('@/views/website/bannerList'), // Parent router-view
        name: 'Banner',
        meta: { title: '轮播图', roles: ['super', 'institute'], icon: 'recordList' },
      },
      {
        path: '/recommendCourse',
        component: () => import('@/views/website/recommendCourse'),
        name: 'RecommendCourse',
        meta: { title: '精品课程', roles: ['super', 'institute'], icon: 'recordList' }
      },
      {
        path: '/announcementList',
        component: () => import('@/views/website/announcementList'),
        name: 'AnnouncementList',
        meta: { title: '公告资讯', roles: ['super', 'institute'], icon: 'recordList' }
      },
    ]
  },
  {
    path: '/system',
    component: Layout,
    redirect: '/instituteList',
    name: 'System',
    meta: { title: '机构管理', roles: ['super'], icon: 'system' },
    children: [
      {
        path: '/instituteList',
        component: () => import('@/views/system/instituteList'),
        name: 'InstituteList',
        meta: { title: '机构列表', roles: ['super'], icon: 'instituteList' }
      },
    ]
  },
  // 404 page must be placed at the end !!!
  { path: '*', redirect: '/404', component: () => import('@/views/404'), hidden: true }
]

const router = new Router({
  // mode: 'history', //后端支持可开
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes
})

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = router
  router.matcher = newRouter.matcher // 重置路由
}

// 解决路由跳转重复报错问题
const originalPush = Router.prototype.push
Router.prototype.push = function push(location, onResolve, onReject) {
  if (onResolve || onReject) return originalPush.call(this, location, onResolve, onReject)
  return originalPush.call(this, location).catch(err => err)
}

export default router
